package com.thlws.study.rabbitmq.internal.receiver;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author HanleyTang 2019-06-23
 */
@Component
public class FanoutReceiver {

    private final Logger logger = LoggerFactory.getLogger(getClass());


    /***
     *  自动创建队列，Exchange（FANOUT） 与 Queue绑定
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanoutQueue1"),
            exchange = @Exchange(value = "myFanoutExChange",type = ExchangeTypes.FANOUT)
    ))
    public void fanoutQueue1(Message message, Channel channel) throws IOException {
        logger.info("Fanout-queue1 收到: {}", message);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanoutQueue2"),
            exchange = @Exchange(value = "myFanoutExChange",type = ExchangeTypes.FANOUT)
    ))
    public void fanoutQueue2(Message message, Channel channel) throws IOException {
        logger.info("Fanout-queue2 收到: {}", message);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
